package com.gy.springboot.config;

import java.util.Arrays;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.gy.springboot.filter.MyFilter;
import com.gy.springboot.listener.MyListener;
import com.gy.springboot.servlet.MyServlet;

@Configuration
public class MyServerConfig {

    // 注册三大组件

    // 1、注册servlet
    // 自定义注册的servlet不受Spring Mvc拦截器拦截 同样可以配置filter过滤处理
    @Bean
    public ServletRegistrationBean<MyServlet> myServlet() {
        ServletRegistrationBean<MyServlet> registrationBean = new ServletRegistrationBean<MyServlet>(
                new MyServlet(), "/my/servlet");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<MyFilter> myFilter() {
        FilterRegistrationBean<MyFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello", "/my/servlet"));
        return registrationBean;

    }

    @Bean
    public ServletListenerRegistrationBean<MyListener> myListener() {
        ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean = new ServletListenerRegistrationBean<MyListener>(
                new MyListener());
        return servletListenerRegistrationBean;
    }

}
